/*
 * Copyright (c) 2025 彭友聪
 * nativePC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2. 
 * You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
 * See the Mulan PSL v2 for more details.  
 * 
 * Author: 彭友聪 
 * email：2923616405@qq.com 
 * date: 2025/11/2 07:48
 * file: ${FILE_NAME}
 * product: DevEco Studio
 * LICENSE: MulanPSL-2.0
 * */
//
// Created on 2025/11/2.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef NATIVEPC_TESTDRAGIMAGEEXAMPLE_H
#define NATIVEPC_TESTDRAGIMAGEEXAMPLE_H
#include <arkui/drag_and_drop.h>
#include "ArkUIBaseNode.h"
#include <multimedia/image_framework/image/pixelmap_native.h>
#include "ArkUIColumnNode.h"
#include "ArkUIImageNode.h"
#include "NativeEntry.h"
#include <hilog/log.h>
#include <database/udmf/udmf_meta.h>
namespace NativeModule { 
	std::shared_ptr<ArkUIBaseNode> CreateDragImageExample() {
        auto image = std::make_shared<ArkUIImageNode>();
        image -> SetWidth(200);
        image -> SetHeight(200);
        image -> SetBackgroundColor(0xff112233);
        image -> SetImageSrc();
        image -> SetDraggable(true);
        // 创建参数结构体实例，并设置参数
        OH_Pixelmap_InitializationOptions *createOpts;
        OH_PixelmapInitializationOptions_Create(&createOpts);
        OH_PixelmapInitializationOptions_SetWidth(createOpts, 400);
        OH_PixelmapInitializationOptions_SetHeight(createOpts, 600);
        OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, PIXEL_FORMAT_BGRA_8888);
        OH_PixelmapInitializationOptions_SetAlphaType(createOpts, PIXELMAP_ALPHA_TYPE_UNKNOWN);
        // 创建Pixelmap实例
        uint8_t data[960000];
        size_t dataSize = 960000;
        for (int i = 0; i < dataSize; i++) {
        data[i] = i + 1;
        }
        OH_PixelmapNative *pixelmap = nullptr;
        OH_PixelmapNative_CreatePixelmap(data, dataSize, createOpts, &pixelmap);
        OH_PixelmapNative_Rotate(pixelmap, 45);
        OH_PixelmapNative_Opacity(pixelmap, 0.1);
        OH_PixelmapNative_Scale(pixelmap, 0.5, 1.0);
        OH_PixelmapNative_Translate(pixelmap, 50.0, 10.0);
        image -> SetDragPreview(pixelmap);
        auto *previewOptions = OH_ArkUI_CreateDragPreviewOption();
        auto returnValue2 = OH_ArkUI_DragPreviewOption_SetNumberBadgeEnabled(previewOptions, true);
        auto returnValue3 = OH_ArkUI_DragPreviewOption_SetBadgeNumber(previewOptions, 10);
        auto returnValue4 = OH_ArkUI_DragPreviewOption_SetDefaultRadiusEnabled(previewOptions, true);
        image -> SetDragPreviewOption(previewOptions);
        auto dragActionCallback = [](ArkUI_NodeEvent *event) {
              showUITextCallback("触摸图片", "触发拖拽事件");
              auto eventType = OH_ArkUI_NodeEvent_GetEventType(event);
              auto targetId = OH_ArkUI_NodeEvent_GetTargetId(event);
              auto *dragEvent = OH_ArkUI_NodeEvent_GetDragEvent(event);
              OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "dragTest",
                            "targetId=%{public}d,eventType=%{public}d,", targetId,
                            eventType);
                switch (eventType) {
                    case NODE_ON_DRAG_START:
                        {
                            showUITextCallback("拖拽动作开始", "进入NODE_ON_DRAG_START分支");
                            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "dragTest", "NODE_ON_DRAG_START EventReceiver");
                            OH_UdmfRecord *record = OH_UdmfRecord_Create();
                            int returnValue;
                            OH_UdsFileUri *imageValue = OH_UdsFileUri_Create();
                            returnValue =
                                OH_UdsFileUri_SetFileUri(imageValue, "/../../resources/rawfile/common/logo.png");
                            returnValue = OH_UdmfRecord_AddFileUri(record, imageValue);
                            OH_UdmfData *data = OH_UdmfData_Create();
                            returnValue = OH_UdmfData_AddRecord(data, record);
                            returnValue = OH_ArkUI_DragEvent_SetData(dragEvent, data);
                            break;
                        }
                    case NODE_ON_DROP:
                        {
                            showUITextCallback("拖拽动作结束", "进入NODE_ON_DROP分支");
                            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "dragTest", "NODE_ON_DROP EventReceiver");
                            // 获取UDMF data
                            int returnValue;
                            // 创建OH_UdmfData对象
                            OH_UdmfData *data = OH_UdmfData_Create();
                            returnValue = OH_ArkUI_DragEvent_GetUdmfData(dragEvent, data);
                            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "dragTest",
                                         "OH_ArkUI_DragEvent_GetUdmfData returnValue = %{public}d", returnValue);
                            // 判断OH_UdmfData是否有对应的类型
                            bool resultUdmf = OH_UdmfData_HasType(data, UDMF_META_GENERAL_FILE);
                                if (resultUdmf) {
                                    // 获取OH_UdmfData的记录
                                    unsigned int recordsCount = 0;
                                    OH_UdmfRecord **records = OH_UdmfData_GetRecords(data, &recordsCount);
                                    // 获取records中的元素
                                    int returnStatus;
                                        for (int i = 0; i < recordsCount; i++) {
                                            // 从OH_UdmfRecord中获取文件类型数据
                                            OH_UdsFileUri *imageValue = OH_UdsFileUri_Create();
                                            returnStatus = OH_UdmfRecord_GetFileUri(records[i], imageValue);
                                            const char *fileUri = OH_UdsFileUri_GetFileUri(imageValue);
                                            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "dragTest",
                                                         "dragTest OH_UdmfRecord_GetPlainText "
                                                         "returnStatus= %{public}d "
                                                         "fileUri= %{public}s",
                                                         returnStatus, fileUri);
                                            // 使用结束后销毁指针
                                            OH_UdsFileUri_Destroy(imageValue);
                                        }
                                }
                                else {
                                    OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "dragTest",
                                                 "OH_UdmfData_HasType not contain UDMF_META_GENERAL_FILE");
                                }
                            break;
                        }
                    default:
                        {
                            showUITextCallback("拖拽动作未知", "进入其他分支");
                            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "dragTest", "other drag event");
                        }
                }
        };
        image -> RegisterDragStart(dragActionCallback);
        image -> RegisterDragDrop(dragActionCallback);
        return image;
	}
}

#endif //NATIVEPC_TESTDRAGIMAGEEXAMPLE_H
